#ifndef __UTILITY_SINGLETON_HPP__
#define __UTILITY_SINGLETON_HPP__
#include <boost/scoped_ptr.hpp>
#include <boost/thread/once.hpp>
#include <boost/noncopyable.hpp>
namespace core {
	template <class T>
	class Singleton : private boost::noncopyable
	{
	public:
		static T& GetInstance()
		{
			boost::call_once(init, flag_);
			return *t_;
		}
		static void init() // never throws
		{
			static delete_helper<T> helper;
			helper.ptr = t_ = new T();
		}

		static T* GetInstancePtr()
		{
			boost::call_once(init, flag_);
			return t_;
		};
	protected:
		~Singleton() {}
		Singleton() {}

	private:
		template<class M>
		struct delete_helper 
		{
			delete_helper()
			{
				ptr = NULL;
			}
			~delete_helper()
			{
				delete ptr;
			}
			M* ptr;
		};
		static T* t_;
		static boost::once_flag flag_;
	};

	template<class T> T * Singleton<T>::t_ = NULL;
	template<class T> boost::once_flag Singleton<T>::flag_ = BOOST_ONCE_INIT;

}// namespace core
#endif//__UTILITY_SINGLETON_HPP__